package cn.study.luckyapp.user.query;

import cn.study.luckyclient.dto.data.UserVo;
import cn.study.luckyclient.dto.query.UserLoginQuery;
import cn.study.luckydomain.User.UserEntity;
import cn.study.luckydomain.gateway.UserGateway;
import com.alibaba.cola.exception.SysException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.control.MappingControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class UserLoginQueryExe {
    @Resource(name ="stringRedisTemplate")
    private  RedisTemplate redisTemplate;
    @Autowired
    private  UserGateway userGateway;
    public String execute(UserLoginQuery query) {
        UserEntity userEntity = userGateway.findByUserName(null, query.getUserName());
        if(Objects.isNull(userEntity)){
            throw new SysException("账号不存在");
        }
        if(!userEntity.getPassword().isEqual(query.getPassword())){
            throw new SysException("密码错误");
        }
        String token=null;
        token= UUID.randomUUID().toString().replace("-","");
        Long id = userEntity.getId();
        String ids=new String(String.valueOf(id));
        redisTemplate.opsForValue().set(token,ids,30, TimeUnit.MINUTES);
        String id1 =(String) redisTemplate.opsForValue().get(token);
        System.out.println(id1);
        return token;
    }
}
